package org.ruiyichen.guizhou.agr.ui.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import org.ruiyichen.guizhou.agr.R;
import org.ruiyichen.guizhou.agr.base.BaseActivity;
import org.ruiyichen.guizhou.agr.base.DataManger;
import org.ruiyichen.guizhou.agr.config.Constant;
import org.ruiyichen.guizhou.agr.net.OkHttpManager;
import org.ruiyichen.guizhou.agr.ui.mineview.BToast;
import org.ruiyichen.guizhou.agr.utils.AppUtils;
import org.ruiyichen.guizhou.agr.utils.L;

/**
 * 软件启动界面
 */
public class StartActivity extends BaseActivity {
    private ImageView mLinear;

    public static boolean isForeground = false;

    private boolean hasGotToken = false;
    private boolean canDo = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.activity_start);
        bOkHttpManager = new OkHttpManager(this, bHandler);

        initData();

//        initAccessToken();
//        initAccessTokenWithAkSk();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
                startLogin();
            }
        }, 500);
    }

    private void startLogin() {
        boolean hasLogin = DataManger.getInstance().hasLogin();
        Intent it = new Intent(StartActivity.this, hasLogin
                ? IrrigatedSystemActivity.class : LoginInActivity.class);
        startActivity(it);
    }

    private void initData() {

    }

    @Override
    public void initViews() {

    }

    @Override
    protected void initEvents() {

    }

    @Override
    protected void onResume() {
        isForeground = true;
        super.onResume();
    }


    @Override
    protected void onPause() {
        isForeground = false;
        super.onPause();
    }

    @Override
    protected void onPostHandle(int requestType, Object response, boolean error, int errorCode, Object obj1, Object obj2) {
        super.onPostHandle(requestType, response, error, errorCode, obj1, obj2);
        if (error) {
            BToast.getInstance().show(getResources().getString(R.string.abnormal));
        } else {
            if (response == null) {
                BToast.getInstance().show(getResources().getString(R.string.datafailure));
                startLogin();
            } else {
                if (requestType == Constant.RequestType.SEND_MESSAGE) {

                }
            }
        }
    }

    private boolean isFristRun() {
        L.e("检验是否第一次安装");
        SharedPreferences sharedPreferences = this.getSharedPreferences(
                "share", MODE_PRIVATE);
        boolean isFirstRun = sharedPreferences.getBoolean("isFirstRun", true);
        String versionName = sharedPreferences.getString("versionName", "");
        SharedPreferences.Editor editor = sharedPreferences.edit();
        String vn = AppUtils.getVersionName(this);
        if (versionName.equals(vn)) {
            if (!isFirstRun) {
                return false;
            } else {
                editor.putBoolean("isFirstRun", false);
                editor.commit();
                return true;
            }
        } else {
            editor.putString("versionName", vn);
            editor.putBoolean("isFirstRun", false);
            editor.commit();
            return true;
        }
    }


    @Override
    public void onBackPressed() {
//        super.onBackPressed();
    }

}
